package com.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dao.ClazzDao;
import com.dao.PaperDao;
import com.dao.ProanswerDao;
import com.dao.ProgrammingDao;
import com.dao.UserDao;
import com.model.Clazz;
import com.model.Paper;
import com.model.Proanswer;
import com.model.Programming;
import com.model.User;
import com.service.TeacherService;
import com.util.Pagination;

@Service("TeacherService")
public class TeacherServiceImpl implements TeacherService {

    public ClazzDao getClazzDao() {
        return clazzDao;
    }

    @Resource(name = "ClazzDao")
    public void setClazzDao(ClazzDao clazzDao) {
        this.clazzDao = clazzDao;
    }

    private UserDao userDao;

    private PaperDao paperDao;

    private ProgrammingDao programmingDao;

    private ProanswerDao proanswerDao;

    private ClazzDao clazzDao;

    public ProgrammingDao getProgrammingDao() {
        return programmingDao;
    }

    @Resource(name = "ProgrammingDao")
    public void setProgrammingDao(ProgrammingDao programmingDao) {
        this.programmingDao = programmingDao;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    @Resource(name = "UserDao")
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public PaperDao getPaperDao() {
        return paperDao;
    }

    @Resource(name = "paperDao")
    public void setPaperDao(PaperDao paperDao) {
        this.paperDao = paperDao;
    }

    public ProanswerDao getProanswerDao() {
        return proanswerDao;
    }

    @Resource(name = "ProanswerDao")
    public void setProanswerDao(ProanswerDao proanswerDao) {
        this.proanswerDao = proanswerDao;
    }

    @Override
    public Pagination getPageList(User user, Pagination page, Paper paper) {
        paper.setUser(user);
        page = paperDao.getPaperList(paper, page);
        return page;
    }

    @Override
    public boolean delPaper(int id) {
        boolean result = true;
        Paper paper = new Paper();
        paper.setId(id);

        try {
            paperDao.delete(paper);
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }

        return result;
    }

    @Override
    public void addProgram(Programming programming) {
        if (programming.getRuntime() == null || programming.getRuntime() == 0) {
            programming.setRuntime(3000);
        }
        if (programming.getRuntimeforjava() == null || programming.getRuntimeforjava() == 0) {
            programming.setRuntimeforjava(3000);
        }
        if (programming.getMemoryforc() == null || programming.getMemoryforc() == 0) {
            programming.setMemoryforc(3000);
        }
        if (programming.getMemoryforjava() == null || programming.getMemoryforjava() == 0) {
            programming.setMemoryforjava(3000);
        }
        programmingDao.save(programming);

    }

    @Override
    public void addPaper(Paper paper) {
        paperDao.save(paper);
    }

    @Override
    public Paper findByid(Integer id) {
        return paperDao.findById(id);
    }

    @Override
    public void updatePaper(Paper paper) {

        paperDao.merge(paper);
    }

    @Override
    public void updateProgram(Programming programming) {

        programmingDao.merge(programming);
    }

    @Override
    public Pagination getPrograms(Pagination page, Programming program, String papername, User loginUser) {
        if (program.getTitle() != null && program.getTitle().equals("")) {
            program.setTitle(null);
        }
        if (program.getVisible() != null && program.getVisible().equals("")) {
            program.setVisible(null);
        }
        page = programmingDao.getProgrammingList(program, page, papername, loginUser);
        return page;
    }

    @Override
    public Programming findProByid(int id) {
        return programmingDao.findById(id);
    }

    @Override
    public int countProgram(Programming programming) {
        return programmingDao.countProgramming(programming);

    }

    @Override
    public int countPaper() {
        return paperDao.countPaper();

    }

    @Override
    public Programming findProgram(Programming program) {

        return programmingDao.findProgram(program);
    }

    @Override
    public Pagination StuScore(Proanswer proanswer, Pagination paperPage) {

        return proanswerDao.StuScore(proanswer, paperPage);

    }

    @Override
    public Clazz findClazzById(int parseInt) {
        return clazzDao.findClazzById(parseInt);

    }

    @Override
    public Pagination getMyPageList(User temp, Pagination page, Paper paper) {

        paper.setUser(temp);
        page = paperDao.getMyPaperList(paper, page);
        return page;
    }

    @Override
    public void deletePaperProgram(String paperid, String id) {
        paperDao.deletePaperProgram(paperid, id);

    }

    @Override
    public void ProgramDel(String id) {
        programmingDao.ProgramDel(id);

    }

    @Override
    public int getPaperScore(int parseInt) {

        return paperDao.getPaperScore(parseInt);
    }

    @Override
    public void delPaperClazz(int cid, Integer paperId) {

        clazzDao.delPaperClazz(cid, paperId);

    }

    @Override
    public Pagination getProgramBlanks(Pagination page, Programming program, String papername, User temp) {
        if (program.getTitle() != null && program.getTitle().equals("")) {
            program.setTitle(null);
        }
        if (program.getVisible() != null && program.getVisible().equals("")) {
            program.setVisible(null);
        }
        page = programmingDao.getProgrammingBlankList(program, page, papername, temp);
        return page;
    }

}
